package com.msconfig.device;

import com.geo.parse.GnssDataTime;
import com.geo.parse.GnssLocationData;
import com.geo.parse.GnssRefStationData;
import com.geo.parse.GnssSateInfoList;
import com.geo.parse.GnssSolutionStatus;
import com.msconfig.event.DeviceUpdateUINotify;

import java.util.Calendar;
import java.util.Date;

//卫星信息数据, 定位信息、基站信息、卫星列表等;
public class GeoLocationData extends GnssLocationData
{
	public final static Object locationLock = new Object();
	// 数据是否有效;
	private boolean mbValidate = false;
	// 本地时间;
	private GnssDataTime mLocalTime = new GnssDataTime();
	// 基站信息;
	private GnssRefStationData mRefStationData = null;
	// 卫星列表信息;
	private GnssSateInfoList mSateInfoList = null;
	// 用于单例模式 ;
	private static GeoLocationData mGeoGnssData = null;
	// 单例模式获取实例 ;
	public static GeoLocationData getInstance() {
		if (mGeoGnssData == null) 
		{
			mGeoGnssData = new GeoLocationData();
			mGeoGnssData.setSolutionType(GnssSolutionStatus.ST_NO_DATA);
		}
		return mGeoGnssData;
	}
	
	// 清空所有数据 ;
	public void resetGnssData() {
		if (mGeoGnssData == null) {
			mGeoGnssData = new GeoLocationData();
			mGeoGnssData.setSolutionType(GnssSolutionStatus.ST_NO_DATA);
		}
		else {
			setSolutionType(GnssSolutionStatus.ST_NO_DATA);
			setLatitude(0);
			setLongitude(0);
			setAltitude(0);
			setAgeOfDiff(0);
			setErms(0);
			setNrms(0);
			setHrms(0);
			setVrms(0);
			setHrms(0);
			setVdop(0);
			setAzimuth(0);
			setSensorPitch(0);
			setSensorRoll(0);
			setSensorAzimuth(0);
			setVelocity(0);
			setUndulation(0);
			setNBaseId(0);
			setDatatime(new GnssDataTime());
			mLocalTime = new GnssDataTime();
			mRefStationData = null;
			mSateInfoList = null;
			setTiltState(0);
		}
		DeviceUpdateUINotify.eventUpdateLocationResult();
	}

	private long lastTime = 0;
	// 设置位置信息;
	public void setLocationData(GnssLocationData location) {
		lastTime = System.currentTimeMillis();
		//这是最原始的数据;
		mbValidate = true;
		super.setSolutionType(location.getSolutionType());
		super.setLongitude(location.getLongitude());
		super.setLatitude(location.getLatitude());
		super.setAltitude(location.getAltitude());
		
		super.setDatatime(location.getDatatime());
        super.setHrms(location.getHrms());
		super.setNrms(location.getNrms());
		super.setErms(location.getErms());
		super.setVrms(location.getVrms());
		super.setPdop(location.getPdop());
		super.setHdop(location.getHdop());
		super.setVdop(location.getVdop());
		
		super.setAgeOfDiff(location.getAgeOfDiff());
		super.setVelocity(location.getVelocity());
		super.setAzimuth(location.getAzimuth());
		super.setUndulation(location.getUndulation());
		super.setNBaseId(location.getNBaseId());

		super.setSensorPitch(location.getSensorPitch());
		super.setSensorRoll(location.getSensorRoll());
		super.setSensorAzimuth(location.getSensorAzimuth());
		super.setPhaseHeight(location.getPhaseHeight());
		super.setTiltState(location.getTiltState());
		this.setSensorHRMS(location.getSensorHRMS());
		this.setSensorVRMS(location.getSensorVRMS());

		GnssDataTime temDataTime = getDatatime();

		Calendar date = Calendar.getInstance();
		date.set(temDataTime.getYear(), temDataTime.getMonth()-1,
				temDataTime.getDay(), temDataTime.getHour(),
				temDataTime.getMinute(), temDataTime.getSecond());

		Date localTime = new Date(date.getTime().getTime() + Calendar.getInstance().get(Calendar.ZONE_OFFSET));
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(localTime);
		mLocalTime.setYear(calendar.get(Calendar.YEAR));
		mLocalTime.setMonth(calendar.get(Calendar.MONTH)+1);
		mLocalTime.setDay(calendar.get(Calendar.DAY_OF_MONTH));
		mLocalTime.setHour(calendar.get(Calendar.HOUR_OF_DAY));
		mLocalTime.setMinute(calendar.get(Calendar.MINUTE));
		mLocalTime.setSecond(calendar.get(Calendar.SECOND));
		mLocalTime.setMillisecond(temDataTime.getMillisecond());
	}


	//获取本地时间;
	public GnssDataTime getLacalTime()
	{
		return mLocalTime;
	}
	
	//设置基站信息;
	public void setRefStationData(GnssRefStationData data)
	{
		mRefStationData = data;
	}
	public GnssRefStationData getRefStationData()
	{
		return mRefStationData;
	}
	
	// 设置卫星列表;
	public void setSateInfoList(GnssSateInfoList data)
	{
		mSateInfoList = data;
	}
	public GnssSateInfoList getSateInfoList()
	{
		return mSateInfoList;
	}

	//连接过程中判断有没有数据返回回来，有可能长时间不返回数据
	public boolean isValidate() {
		if (getSolutionType() == GnssSolutionStatus.ST_GEO_TPS_FIX)
			return mbValidate;

		long mCurrentTime = System.currentTimeMillis();
		if (mbValidate && mCurrentTime - lastTime > 2500) {//表示长时间没有定位信息返回，设置为无数据
			mbValidate = false;
			setSolutionType(GnssSolutionStatus.ST_NO_DATA);
			DeviceUpdateUINotify.eventUpdateLocationResult();
		}
		return mbValidate;
	}

}
